package com.aaa.controller;

import com.aaa.entity.User;
import com.aaa.service.MenuService;
import com.aaa.service.UserService;
import com.aaa.util.DefaultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

    @RequestMapping("/checkUser")
    @ResponseBody
    public DefaultMsg checkUser(User user, HttpSession session){
        User user1 = userService.checkUser(user);
        DefaultMsg defaultMsg=new DefaultMsg();
        if(user1==null){
            defaultMsg.setError("用户名或密码错误");
            return defaultMsg;
        }else{
            session.setAttribute("USER",user1);
            return defaultMsg;

        }
    }
    @RequestMapping("/toIndex")
    public String toIndex(){
        return "index";
    }

    @RequestMapping("/toLogout")
    public String toLogout(HttpSession httpSession){
        httpSession.removeAttribute("USER");
        return "redirect:login.jsp";
    }


    @RequestMapping("/myListMenu")
    @ResponseBody
    public Map<String, Object> myListMenu(User user, HttpServletRequest request){
        HttpSession session=request.getSession();
        User user2 = (User) session.getAttribute("USER");
        Map<String, Object> map = menuService.listMiniUiMenus(user2);
        return map;
    }


    @RequestMapping("/toUpdatePassword")
    public String toUpdatePassword(){
        return "user/updatepassword";
    }


    @RequestMapping("/updatePassword")
    @ResponseBody
    public DefaultMsg updatePassword(User user) {
        DefaultMsg defaultMsg=userService.updatePassword(user);
       return defaultMsg;
    }




}
